import os

import qtmodern.styles
import qtmodern.windows
from PySide6.QtCore import Signal, QThread, QDateTime, QTime
from PySide6.QtWidgets import QWidget, QApplication
from common import common
from loading import LoadingAnimation
from ui.aa_ui import Ui_Form


class LongTaskThread(QThread):
    finished = Signal()

    def __init__(self, zd):
        super().__init__()
        self.zd = zd
    def run(self):
        try:
            from pyfile.getData import GetOutlookData
            getOutlookData=GetOutlookData(self.zd)
            getOutlookData.get_data()
        except Exception as e:
            common.show_message(e, 0)
        finally:
            self.finished.emit()


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        # 获取当前日期时间
        current_date_time = QDateTime.currentDateTime()
        # 设置日期为今天，同时将小时、分钟和秒设置为0
        current_date_time.setDate(current_date_time.date())
        current_date_time.setTime(QTime(0, 0, 0))
        self.ui.dateS.setDateTime(current_date_time)
        current_date_time.setTime(QTime(23, 59, 59))
        self.ui.dateE.setDateTime(current_date_time)
        self.bind()

    def bind(self):
        self.ui.pushButton.clicked.connect(self.click)

    def click(self):
        rad = ''
        if self.ui.rad_hr.isChecked():
            rad = '慧镕回货'
        elif self.ui.rad_rj.isChecked():
            rad = '瑞吉'
        elif self.ui.rad_bs.isChecked():
            rad = '保税备件已返回东莞蓝思保税仓'
        elif self.ui.rad_csd.isChecked():
            rad = '畅思得回货'
        self.zd = {'dateS': self.ui.dateS.dateTime().toPyDateTime(), 'dateE': self.ui.dateE.dateTime().toPyDateTime(), 'rad': rad}
        self.ui.longTaskThread = LongTaskThread(self.zd)
        self.ui.finished = self.ui.longTaskThread.finished.connect(self.on_finished)
        current_dir = os.path.dirname(os.path.abspath(__file__))  # 获取当前文件所在目录路径
        self.ui.LoadingAnimation = LoadingAnimation(os.path.join(current_dir, 'gif', '33.gif'))
        self.ui.pushButton.setEnabled(False)
        self.ui.LoadingAnimation.show()
        self.ui.longTaskThread.start()

    def on_finished(self):
        self.ui.pushButton.setEnabled(True)
        self.ui.LoadingAnimation.hide()


if __name__ == '__main__':
    app = QApplication([])
    window = MainWindow()
    qtmodern.styles.dark(app)
    mw = qtmodern.windows.ModernWindow(window)
    mw.move(app.primaryScreen().size().width() / 2 - window.size().width() / 2,
            app.primaryScreen().size().height() / 2 - window.size().height() / 2)
    mw.show()
    app.exec()
